package com.youyi.flymewallpager.apdater;

import android.content.Context;
import android.service.voice.VoiceInteractionService;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by xzp on 16/6/13.
 */
public abstract class BaseRecyclerAdapter<E> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<E> mList;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private ExtraHolder mHeaderHolder,mFooterHolder;
    public BaseRecyclerAdapter(Context context) {
        this(context,null);
    }
    public BaseRecyclerAdapter(Context context,List<E> list){
        mContext=context;
        mList=list;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==-1)
            return mHeaderHolder;
        else if(viewType==-2)
            return mFooterHolder;
        else
            return onCreateCustomViewHolder(parent,viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType=getItemViewType(position);
        if(viewType==-1 || viewType==-2)
            return;
        onBindCustomViewHolder(holder,position);
    }
    public int getListSize(){
        return mList==null?0:mList.size();
    }
    public Context getContext(){
        return mContext;
    }
    public List<E> getList(){
        return mList;
    }
    public void setList(List<E> list){
        this.mList=list;
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        int count=getListSize();
        if(hasHeader())
            count++;
        if(hasFooter())
            count++;
        return count;
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public void setOnItemLongClickListener(OnItemLongClickListener listener){
        mOnItemLongClickListener=listener;
    }
    @Override
    public final int getItemViewType(int position) {
        if(hasHeader() && position==0)
            return -1;
        if(hasFooter() && position==getItemCount()-1)
            return -2;
        return getViewType(position);
    }
    public boolean hasHeader(){
        return mHeaderHolder!=null;
    }
    public boolean hasFooter(){
        return mFooterHolder!=null;
    }
    /**
     * 设置头部
     * @param view
     */
    public void setHeaderView(View view){
       setHeaderView(view,null);
    }
    public void setHeaderView(View view,RecyclerView.LayoutParams params){
        if(view==null){
            mHeaderHolder=null;
        }else {
            if(params!=null)
                view.setLayoutParams(params);
            mHeaderHolder=new ExtraHolder(view);
        }
    }
    /**
     * 设置尾部
     * @param view
     */
    public void setFooterView(View view) {
        setFooterView(view,null);
    }
    public void setFooterView(View view, RecyclerView.LayoutParams params){
        if(view==null){
            mFooterHolder=null;
        }else {
            if(params!=null)
                view.setLayoutParams(params);
            mFooterHolder=new ExtraHolder(view);
        }
    }
    /**
     * 获取viewType
     * @param position
     * @return
     */
    public int getViewType(int position){
        return 0;
    }
    public abstract RecyclerView.ViewHolder onCreateCustomViewHolder(ViewGroup parent, int viewType);
    public abstract void onBindCustomViewHolder(RecyclerView.ViewHolder holder, int position);


    private final class ExtraHolder extends RecyclerView.ViewHolder{
        public ExtraHolder(View itemView) {
            super(itemView);
        }
    }
    public abstract class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{
        public BaseViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(mOnItemClickListener!=null){
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION)
                    mOnItemClickListener.onItemClick(v, position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if(mOnItemLongClickListener!=null) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION)
                    return mOnItemLongClickListener.onItemLongClick(v, position);
            }
            return false;
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public interface OnItemLongClickListener{
        boolean onItemLongClick(View view, int position);
    }
}
